package com.atgugiu.seckill.controller;

import com.atgugiu.seckill.service.SecKillService;
import com.atgugiu.seckill.to.SeckillSkuRedisTo;
import com.atguigu.common.utils.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class SecKillController {

  @Resource
  private SecKillService secKillService;

  /**
   * 当前时间可以参与秒杀的商品信息
   *
   * @return
   */
  @GetMapping(value = "/getCurrentSeckillSkus")
  @ResponseBody
  public R getCurrentSeckillSkus() {
    //获取到当前可以参加秒杀商品的信息
    List<SeckillSkuRedisTo> vos = secKillService.getCurrentSeckillSkus();

    return R.ok().setData(vos);
  }

  @ResponseBody
  @GetMapping(value = "/getSeckillSkuInfo/{skuId}")
  public R getSeckillSkuInfo(@PathVariable("skuId") Long skuId) {
    SeckillSkuRedisTo to = secKillService.getSeckillSkuInfo(skuId);
    return R.ok().setData(to);
  }


  @GetMapping("/kill")
  public String kill(@RequestParam("killId") String killId,
                     @RequestParam("key") String key,
                     @RequestParam("num") Integer num,
                     Model model) {
    String orderSn = null;
    try {
      orderSn = secKillService.kill(killId, key, num);
      model.addAttribute("orderSn", orderSn);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return "success";
  }


}
